//
//  FindPasswordTableViewCellFirst.h
//  CommunityNannyO2O
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "FindPasswordTableViewCellFirst.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "CustomTextField.h"
#import "AppConfig.h"
#import "FindPasswordTableViewCellFirst.h"
#import "QXNetWorkTool.h"
#import "CommFunc.h"

@interface FindPasswordTableViewCellFirst()<QXNetWorkToolDelegate>
{
    NSInteger timeCounter;
    NSTimer *timer;
}

@property (weak, nonatomic) IBOutlet CustomTextField  *phone;

@property (weak, nonatomic) IBOutlet CustomTextField  *verificationCode;

@property (weak, nonatomic) IBOutlet UIButton *sendVerficationCode;

@property (weak, nonatomic) IBOutlet UIButton *verfication;

@end

@implementation FindPasswordTableViewCellFirst

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"FindPasswordTableViewCellFirst";
    FindPasswordTableViewCellFirst *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil]firstObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}

-(void)awakeFromNib
{
    //placeholder字体、颜色
    [self.phone setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:15]
                  Color:UIColorRGBA(4, 4, 4, 1)];
    self.phone.placeholder = @"请输入您的手机号码";
    
    [self.verificationCode setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:13] Color:UIColorRGBA(174, 174, 174, 1)];
    
    //叉号，一次性删除输入框中的内容
    [self.phone setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.verificationCode setClearButtonMode:UITextFieldViewModeWhileEditing];
    
    //数字键盘
    self.phone.keyboardType = UIKeyboardTypeNumberPad;
    self.verificationCode.keyboardType = UIKeyboardTypeNumberPad;
    
    //编辑时清空
    self.phone.clearsOnBeginEditing = YES;
    self.verificationCode.clearsOnBeginEditing = YES;
    
    //
    self.verfication.layer.cornerRadius = 5;
    self.verfication.layer.masksToBounds = YES;
    [self.verfication.layer setBorderWidth:1.0]; //边框宽度
    [self.verfication.layer setBorderColor:UIColorRGBA(255, 255, 255, 1).CGColor];//边框颜色
    [self.verfication addTarget:self action:@selector(verfication:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.sendVerficationCode addTarget:self action:@selector(verficationCodeSender:) forControlEvents:UIControlEventTouchUpInside];
    
    //手势
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self addGestureRecognizer:tapGestureRecognizer];
    
    //子view
    UIView *containView1 = [self viewWithTag: 1];
    
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView1 addGestureRecognizer:tapGestureRecognizer1];
    
    //子view
    UIView *containView2 = [self viewWithTag: 2];
    
    UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView2 addGestureRecognizer:tapGestureRecognizer2];
}

-(void)tap
{
    [self.phone resignFirstResponder];
    [self.verificationCode resignFirstResponder];
}

- (BOOL)isCorrectPhone
{
    if ([self.phone.text isEqualToString:@""])
    {
        [MBProgressHUD showError:@"请输入手机号码" toView:self];
        return NO;
    }
    
    if ([CommFunc isPureInt:self.phone.text])
    {
        if(![CommFunc isValidateMobile:self.phone.text])
        {
            [MBProgressHUD showError:@"请正确输入手机号" toView:self];
            
            return NO;
        }
    }
    else
    {
        [MBProgressHUD showError:@"请正确输入手机号" toView:self];
        
        return NO;
    }
    
    return YES;
}


-(void)timeFireMethod
{
    if (timeCounter < 0)
    {
        return;
    }
    
    timeCounter--;
    
    [self.sendVerficationCode setTitleColor:UIColorRGBA(230, 69, 68, 1) forState:UIControlStateNormal];
    
    self.sendVerficationCode.titleLabel.font =
    [UIFont fontWithName:DefaultFont size:12];
    
    [self.sendVerficationCode setTitle:[NSString stringWithFormat:@"重新发送(%ld)",(long)timeCounter] forState:UIControlStateNormal];
    
    self.sendVerficationCode.userInteractionEnabled = NO;
    
    if(timeCounter <= 0)
    {
        [timer invalidate];
        timer = nil;
        [self.sendVerficationCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.sendVerficationCode.userInteractionEnabled = YES;
    }
}

- (void)verfication:(id)sender
{
//    
//    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
//    
//    QXnetWorkTool.QXNetWorkDelegate = self;
    
    //    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:LoginInterface andParameter:@[@{@"phone":_phone.text}] tag:2];
}

- (void)verficationCodeSender:(id)sender
{
//    if ([self isCorrectPhone] == NO)
//    {
//        return;
//    }
    
//    _phone.text = @"13752725197";
//    
//    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
//    
//    QXnetWorkTool.QXNetWorkDelegate = self;
//    
//    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:VerificationCodeInterface andParameter:@[@{@"phone":_phone.text}] tag:1];
    
}

@end




















